package cn.zuo.view;

import cn.zuo.dao.UserMapper;
import cn.zuo.pojo.Message;
import cn.zuo.pojo.User;
import cn.zuo.service.Check;
import cn.zuo.service.Talking;
import cn.zuo.service.UserService;
import cn.zuo.service.impl.UserServiceImpl;
import cn.zuo.utils.MySqlSession;
import cn.zuo.utils.TalkingPool;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.function.Consumer;
/**
 * @Author 白白旧维
 * @Date 2021/7/7 11:13
 */
public class ServiceMain {
    private static final Logger logger = LoggerFactory.getLogger(ServiceMain.class);//得到logger变量

    private JPanel mjp;
    private JButton btn_on;
    private JButton btn_off;
    private JTextArea textArea1;
    private JScrollPane jsp1;
    private JButton btn_redis_ctrl;
    private Check check;
    private boolean isOpen = false;

    public ServiceMain() {
        btn_on.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isOpen){
                    JOptionPane.showMessageDialog(null,"服务已经开启");
                    return;
                }
                isOpen=true;
                JOptionPane.showMessageDialog(null,"开启服务!!");
                System.out.println("开启服务!!");
                logger.info("开启服务!!");
                String seed = UUID.randomUUID().toString();//aes公钥,需要RSA加密(使用登录时客户端的公钥),需要签名吗？
                check = new Check();
                check.setSeed(seed);
                try {
                    check.connect(textArea1);//信息中转监听

                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });


        btn_off.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"关闭服务!!");
                check.close();
                isOpen=false;
            }
        });


        btn_redis_ctrl.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                HashMap<String, Talking> talkings = TalkingPool.getMap();//修改所有redis控制
                Set<String> talkings_key = talkings.keySet();
                String isOpen = "false";
                if("开启缓存".equals(btn_redis_ctrl.getText())){
                    isOpen = "true";
                    btn_redis_ctrl.setText("关闭缓存");
                    logger.info("开启缓存");
                }else if("关闭缓存".equals(btn_redis_ctrl.getText())){
                    isOpen = "false";
                    btn_redis_ctrl.setText("开启缓存");
                    logger.info("关闭缓存");
                }
                try {
                    for (String key :
                            talkings_key) {
                        Message msg = new Message("server",key,isOpen,"redis");

                        ObjectOutputStream oos = new ObjectOutputStream(talkings.get(key).getS().getOutputStream());
                        oos.writeObject(msg);
                    }

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public JPanel getMjp() {
        return mjp;
    }

    public static void main(String[] args) throws Exception {


        //Nimbus风格
        String lookAndFeel = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);

        JFrame jFrame = new JFrame();
        ServiceMain serviceMain = new ServiceMain();
        jFrame.setContentPane(serviceMain.getMjp());

        jFrame.setLocation(500,200);
        jFrame.setVisible(true);
        jFrame.pack();
        jFrame.setTitle("服务器主界面  <©2021 白白旧维>");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }




}
